using System;
using Microsoft.WindowsMobile.PocketOutlook;

namespace PlaxoSyncWM {
    public class Contact {
        private readonly string firstName;
        private readonly string lastName;
        private readonly string mobileNumber;
        private string itemId;

        public Contact(string fname, string lname, string mobile1)
        {
            firstName = fname;
            lastName = lname;
            mobileNumber = mobile1;
            itemId = string.Empty;
        }

        public Contact(string fname, string lname, string mobile1, string itemId)
        {
            firstName = fname;
            lastName = lname;
            mobileNumber = mobile1;
            this.itemId = itemId;
        }

        public string FirstName {
            get { return firstName; }
        }

        public string LastName {
            get { return lastName; }
        }

        public string MobileNumber {
            get { return mobileNumber; }
        }

        public string ItemId
        {
            get { return itemId; }
        }

        public void Persist() {
            var outlookSession = new OutlookSession();
            var outlookContact = new Microsoft.WindowsMobile.PocketOutlook.Contact
                         {
                             FirstName = FirstName,
                             LastName = LastName,
                             FileAs = (LastName + "," + FirstName),
                             MobileTelephoneNumber = MobileNumber
                         };
            outlookSession.Contacts.Items.Add(outlookContact);
            itemId = outlookContact.ItemId.ToString();
            outlookSession.Dispose();
        }

        public static Contact FromOutlookContact(Microsoft.WindowsMobile.PocketOutlook.Contact contact) {
            return new Contact(contact.FirstName, contact.LastName, contact.MobileTelephoneNumber, contact.ItemId.ToString());
        }
    }
}